<script src="../../router/index.js"></script>
<template>
  <div class="login_container">

    <div class="login_box">
      <!--头像区域-->
      <div class="title">数字化智能管理平台</div>
      <div class="avatar_box">
        <!--<img src="../assets/logo.png" alt="">-->
      </div>
      <!--登录表单区域-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <el-form-item prop="orgid">
          <el-input autocomplete="" v-model.number="loginForm.orgid" placeholder="请输入用户名"
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"
                    prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'login',
    data() {
      return {
        loginForm: {
          orgid: '',
          password: ''
        },
        loginFormRules: {
          username: [
            {required: true, message: '请输入账号', trigger: 'blur'},
            {type: 'number', message: '输入格式不正确'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      login() {
        let data = JSON.stringify(this.loginForm)
        this.$http.post(
          '/api/subLogin',
          data, {
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => {
            if (res.data.code === 200) {
              let jwt = require('jsonwebtoken')
              let str = jwt.decode(res.data.data)
              console.log(str)
              window.sessionStorage.setItem('company', str.orgName)
              window.sessionStorage.setItem('orgid', str.orgid)
              window.sessionStorage.setItem('token', res.data.data)
              window.sessionStorage.setItem('userCount', str.userCount)
              if (str.orgid === 1001){
                window.sessionStorage.setItem('isBoss', '1001')
                this.$router.push("/company")
              }else {
                this.$router.push("/homeIndex")
              }
              this.$message({
                type: "success",
                message: '登录成功'
              })
            }else {
              this.$message({
                type: "error",
                message: res.data.data
              })
            }
        })
      }
    }
  }
</script>

<style scoped>
  .login_container {
    /*background-color: white;*/
    background-image: url('../../assets/img/bg7.jpg');
    background-size: cover;
    background-position: center;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .login_box {
    background-color: rgba(255, 255, 255, 1);
    border-radius: 10px;
    padding: 40px;
  }

  .login_form {
    width: 70%;
    margin: 0 auto;
  }

  .buttons {
    display: flex;
    justify-content: flex-end;
  }

  .title {
    text-align: center;
    font-size: 24px;
    color: black;
    margin-bottom: 30px;
    letter-spacing: 3px;
    margin-bottom: 40px;
  }
</style>
